package com.example.book_shop_java.controller;

import com.alibaba.fastjson.JSON;
import com.example.book_shop_java.bean.User;
import com.example.book_shop_java.dao.UserDao;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
public class UserController {
    @Resource
    UserDao userDao;
    @RequestMapping("/getAllUser")
    public String getAllUser(@RequestParam("username") String username){
        List<User> users = userDao.getAllUser("%"+username+"%");
        String i ="error";
        int code = 413;
        HashMap<String, Object> res = new HashMap<>();
        if(users.size() > 0){
            i ="success";
            code = 200;
        }
        res.put("users",users);
        res.put("code",code);
        res.put("i",i);
        String allUser = JSON.toJSONString(res);
        return allUser;
    }

    @RequestMapping("/addUser")
    public String addUser(@RequestParam("username") String username,
                          @RequestParam("password") String password,
                          @RequestParam("userTel") String userTel,
                          @RequestParam("email") String email){
        int i = userDao.addUser(username,password,userTel,email);
        String message = "error";
        int code = 413;

        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("code",code);
        res.put("message",message);
        String addMessage = JSON.toJSONString(res);
        return addMessage;
    }

    @RequestMapping("/deleteUser")
    public String deleteUser(int user_id){
        int i = userDao.deleteUser(user_id);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);

        String deleteMessage = JSON.toJSONString(res);
        return deleteMessage;
    }


    @RequestMapping("/editUser")
    public String editUser(int user_id,
                           @Param("username") String username,
                           @Param("password") String password,
                           @Param("userTel") String userTel,
                           int state,
                           @Param("role") String role,
                           @Param("email") String email){
        int i = userDao.editUser(user_id,username,password,userTel,state,role,email);
        String message = "error";
        int code = 413;
        if(i > 0){
            message = "success";
            code = 200;
        }
        HashMap<String, Object> res = new HashMap<>();
        res.put("message",message);
        res.put("code",code);

        String editMessage = JSON.toJSONString(res);
        return editMessage;
    }

}
